﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace assignment6
{
    public class OrderDetail
    {
        public string product_name;
        public int quantity;
        public double unit_price;

        public OrderDetail(string product_name, int quantity, double unit_price)
        {
            this.product_name = product_name;
            this.quantity = quantity;
            this.unit_price = unit_price;
        }
        public override bool Equals(object? obj)
        {
            return obj is OrderDetail detail &&
                   product_name == detail.product_name &&
                   quantity == detail.quantity &&
                   unit_price == detail.unit_price;
        }
        public override int GetHashCode()
        {
            return HashCode.Combine(product_name, quantity, unit_price);
        }
        public override string ToString()
        {
            return $"Product Name: {product_name}, Quantity: {quantity}, Unit Price: {unit_price}";
        }
    }
    public class Order
    {
        public List<OrderDetail> list;
        public int order_id;
        public string customer_name;
        public decimal total_price => list.Sum(x => x.quantity * (decimal)x.unit_price);

        public Order(List<OrderDetail> list, int order_id, string customer_name)
        {
            this.list = list;
            this.order_id = order_id;
            this.customer_name = customer_name;
        }
        public override bool Equals(object? obj)
        {
            return obj is Order order &&
                   EqualityComparer<List<OrderDetail>>.Default.Equals(list, order.list) &&
                   order_id == order.order_id &&
                   customer_name == order.customer_name &&
                   total_price == order.total_price;
        }
        public override int GetHashCode()
        {
            return HashCode.Combine(list, order_id, customer_name, total_price);
        }
        public override string ToString()
        {
            return $"Order ID: {order_id}, Customer Name: {customer_name}, Total Price: {total_price}";
        }
    }
    public class OrderService
    {
        public List<Order> orders;
        public OrderService()
        {
            orders= new List<Order>();
        }
        public OrderService(List<Order> orders)
        {
            this.orders = orders;
        }
        public void AddOrder(Order order)
        {
            orders.Add(order);
        }
        public void RemoveOrder(int order_id)
        {
            var removed = orders.RemoveAll(x => x.order_id == order_id);
            if (removed == 0)
                throw new Exception("RemoveOrder:  Order not found");
        }
        public void UpdateOrder(int order_id, Order order)
        {
            var removed = orders.RemoveAll(x => x.order_id == order_id);
            if (removed == 0)
                throw new Exception("UpdateOrder:  Order not found");
            AddOrder(order);
        }
        public List<Order> SearchOrder(string customer_name)
        {
            return (from n in orders where n.customer_name == customer_name select n).ToList();
        }
        public List<Order> SearchOrder(decimal total_price)
        {
            return (from n in orders where n.total_price == total_price select n).ToList();
        }
        public List<Order> SearchOrder(int order_id)
        {
            return (from n in orders where n.order_id == order_id select n).ToList();
        }
    }
}

